package cn.wolfcode.p2p.business.domain;

import cn.wolfcode.p2p.base.domain.BaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.util.Constants;
import com.alibaba.druid.support.json.JSONUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static cn.wolfcode.p2p.util.Constants.*;

/**
 * 借款
 */
@Getter
@Setter
public class BidRequest extends BaseDomain {

    //版本(乐观锁)
    private Integer version = 0;
    //还款方式
    private Integer returnType;
    //借款类型
    private Integer bidRequestType;
    //借款状态
    private Integer bidRequestState;
    //借款金额
    private BigDecimal bidRequestAmount;
    //借款利率
    private BigDecimal currentRate;
    //最小投标金额
    private BigDecimal minBidAmount;
    //还款期限
    private Integer returnMonthes;
    //投标次数
    private Integer bidCount = 0;
    //总利息
    private BigDecimal totalRewardAmount = Constants.ZERO;
    //当前投标的总额
    private BigDecimal currentSum = Constants.ZERO;
    //借款标题
    private String title;
    //借款描述
    private String description;
    //风控意见
    private String note;
    //招标到期时间
    private Date disableDate;
    //招标天数
    private Integer disableDays;
    //借款人
    private LoginInfo createUser;
    //投标记录
    private List<Bid> bids;
    //申请时间
    private Date applyTime;
    //发布时间
    private Date publishTime;

    private Long id;

    //id生成hashcode
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BidRequest that = (BidRequest) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id);
    }

    public String getJsonString() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("username", createUser.getNickName());
        map.put("title", title);
        map.put("bidRequestAmount", bidRequestAmount);
        map.put("currentRate", currentRate);
        map.put("returnMonthes", returnMonthes);
        map.put("returnType", getReturnTypeName());
        map.put("totalRewardAmount", totalRewardAmount);

        return JSONUtils.toJSONString(map);
    }

    /**
     * 还需金额
     *
     * @return
     */
    public BigDecimal getRemainAmount() {
        return bidRequestAmount.subtract(currentSum);
    }

    /**
     * 显示投标进度
     *
     * @return
     */
    public int getPersent() {
        return currentSum.divide(bidRequestAmount, Constants.SCALE_CALCULATE, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).intValue();
    }

    /**
     * 还款类型
     *
     * @return
     */
    public String getReturnTypeName() {
        return returnType == Constants.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL ? "按月分期" : "按月到期";

    }

    //标的类型
    public String getBidRequestTypeDisplay() {
        switch (bidRequestType) {
            case BIDREQUEST_TYPE_NORMAL:
                return "信用标";
            case BIDREQUEST_TYPE_TRANSFER:
                return "债权标";
            case BIDREQUEST_TYPE_EXP:
                return "体验标";
        }
        return "状态异常";
    }


    //借款状态显示
    public String getBidRequestStateDisplay() {
        switch (bidRequestState) {
            case BIDREQUEST_STATE_APPLYING:
                return "申请中";
            case BIDREQUEST_STATE_PUBLISH_PENDING:
                return "待发布";
            case BIDREQUEST_STATE_BIDDING:
                return "招标中";
            case BIDREQUEST_STATE_UNDO:
                return "已撤销";
            case BIDREQUEST_STATE_BIDDING_OVERDUE:
                return "流标";
            case BIDREQUEST_STATE_APPROVE_PENDING_1:
                return "满标1审";
            case BIDREQUEST_STATE_APPROVE_PENDING_2:
                return "满标2审";
            case BIDREQUEST_STATE_REJECTED:
                return "满标审核被拒绝";
            case BIDREQUEST_STATE_PAYING_BACK:
                return "还款中";
            case BIDREQUEST_STATE_COMPLETE_PAY_BACK:
                return "已还清";
            case BIDREQUEST_STATE_PAY_BACK_OVERDUE:
                return "逾期";
            case BIDREQUEST_STATE_PUBLISH_REFUSE:
                return "发标审核拒绝状态";
        }
        return "状态异常";
    }

}
